import 'package:flutter/material.dart';
import 'package:flutterapp/Base/UIComponent/RefreshLoadConfig.dart';
import 'package:flutterapp/Base/net/ApiMapper.dart';
import 'package:flutterapp/InitConfig.dart';

class DemoPageRefreshLoad extends StatefulWidget {
  static const ROUTER_NAME = "DemoPageRefreshLoad";

  @override
  _DemoPageRefreshLoadState createState() => _DemoPageRefreshLoadState();
}

class _DemoPageRefreshLoadState extends State<DemoPageRefreshLoad> {
  List dataList = [1, 2, 3];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("data"),
      ),
      body: RefreshLoadWrapper(
        ListView.builder(
          itemCount: dataList.length,
          itemBuilder: (context, index) => _buildItem(dataList[index]),
        ),
        (page, callBack) {
          dataList.clear();
          myHttp.postForm(ApiMapper.testUrl, data: {
            "username": "年轻人不讲武德",
            "password": "bigENoneFlash",
            "repassword": "bigENoneFlash",
          }).then((res) {
            int x = dataList.length;
            int limit = dataList.length + 10;
            while (x < limit) {
              dataList.add(x);
              x++;
            }
            callBack.finishRefresh();
            setState(() {});
          });
        },
        (page, callBack) {
          myHttp.postForm(ApiMapper.testUrl, data: {
            "username": "年轻人不讲武德",
            "password": "bigENoneFlash",
            "repassword": "bigENoneFlash",
          }).then((res) {
            int x = dataList.length;
            int limit = dataList.length + 10;
            while (x < limit) {
              dataList.add(x);
              print(x);
              x++;
            }
            callBack.finishLoad();
            setState(() {});
          });
        },
      ),
    );
  }

  _buildItem(item) {
    return Card(
      child: Container(
        child: Text("$item"),
        height: 50,
      ),
    );
  }
}
